package cn.kone.blog.controller;


import cn.kone.base.utlis.R;
import cn.kone.blog.domain.Article;
import cn.kone.blog.domain.query.ArticleQuery;
import cn.kone.blog.dto.ArticleDTO;
import cn.kone.blog.dto.ArticleGDDTO;
import cn.kone.blog.service.IArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章接口类")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public R<List<Article>> findAllArticles() {

        List<Article> all = articleService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<Article> findOneArticles(@PathVariable("id") Long id) {
        Article one = articleService.findOne(id);
        return R.ok(one);
    }

    @PutMapping
    @ApiOperation(value = "新增和修改")
    public R<Void> addAndUpdateArticle(@RequestBody Article article) {
        Long id = article.getId();
        try {
            if (id == null) {
                articleService.addArticle(article);
            } else {
                articleService.updateArticle(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteArticles(@PathVariable("id") Long id) {
        try {
            articleService.deleteArticle(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param articleQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageInfo<Article> pageQuery(@RequestBody ArticleQuery articleQuery) {

        List<Article> list = articleService.pageQuery(articleQuery);
        return new PageInfo<Article>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchDeleteArticle(@RequestBody Long[] ids) {
        try {
            articleService.batchDeleteArticle(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 文章归档查询
     * @return 返回文章时间,文章数量
     */
    @GetMapping("/articleTimeStatistics")
    @ApiOperation(value = "文章归档统计")
    public List<ArticleDTO> articleTimeStatistics() {
        return articleService.articleTimeStatistics();
    }

    /**
     * 相关文章查询
     * @return
     */
    @GetMapping("/relateArticle/{id}")
    @ApiOperation(value = "相关文章查询")
    public List<Article> relateArticle(@PathVariable("id") Long id) {
        return articleService.relateArticle(id);
    }

    /**
     * 头部栏归档查询
     * @param time
     * @return
     */
    @GetMapping("/getBlogGdList/{time}")
    public List<ArticleGDDTO>getBlogGdList(@PathVariable("time") String time) {
        return articleService.articleGD();
    }
}
